//
//  LFBTabBarController.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/9.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBTabBarController.h"
#import "LFBNavigationController.h"
#import "UIColor+Addition.h"
#import "LFBShoppingCartController.h"

@interface LFBTabBarController () <UITabBarControllerDelegate>

@end

@implementation LFBTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIViewController *homeController = [self createControllerWithImageName:@"v2_home" andClassName:@"LFBHomeController" andTitle:@"首页"];
    
    UIViewController *marketController = [self createControllerWithImageName:@"v2_order" andClassName:@"LFBMarketController" andTitle:@"闪电超市"];
    
    UIViewController *shoppingCartController = [self createControllerWithImageName:@"shopCart" andClassName:@"LFBBaseViewController" andTitle:@"购物车"];
    
    UIViewController *mineController = [self createControllerWithImageName:@"v2_my" andClassName:@"LFBMineController" andTitle:@"我的"];
    
    self.viewControllers = @[homeController,marketController,shoppingCartController,mineController];
    
    //文字及图片渲染颜色
    self.tabBar.tintColor = [UIColor redColor];
    self.tabBar.tintColor = [UIColor colorWithHex:0x8A6649 alpha:1.0];
    //标签栏颜色
    self.tabBar.barTintColor = [UIColor colorWithWhite:1 alpha:0.8];
    
    //设置代理
    self.delegate = self;
}

- (UIViewController *)createControllerWithImageName:(NSString *)imageName andClassName:(NSString *)className andTitle:(NSString *)title
{
    Class class = NSClassFromString(className);
    
    UIViewController *vc = [[class alloc] init];
    
    //设置文字
    vc.tabBarItem.title = title;
    //设置未选中图片
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    //设置已选中图片
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"_r"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //定义每个控制器的navigationBar
    LFBNavigationController *nav = [[LFBNavigationController alloc] initWithRootViewController:vc];
    
    //设置navigationItem的标题
    vc.navigationItem.title = title;
    
    return nav;
}

#pragma - UITabBarControllerDelegate 代理方法 -
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    LFBBaseNavigationController *baseNav = (LFBBaseNavigationController *)viewController;
//    NSLog(@"~~~%@",NSStringFromClass([baseNav.childViewControllers.firstObject class]));
    
    NSString *classStr = NSStringFromClass([baseNav.childViewControllers.firstObject class]);

    if([classStr isEqualToString:@"LFBBaseViewController"])
    {
        //模态控制器
        LFBShoppingCartController *shoppingVC = [LFBShoppingCartController new];
        shoppingVC.view.backgroundColor = [UIColor whiteColor];
        
        LFBBaseNavigationController *modelNav = [[LFBBaseNavigationController alloc] initWithRootViewController:shoppingVC];
        [self presentViewController:modelNav animated:YES completion:nil];
        
        return NO;
    }
    
    return YES;
}

@end
